package chat.fragments;

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.whu.zj.R;

import java.util.ArrayList;
import java.util.List;

import chat.activities.ChatActivity;
import chat.activities.RoomChatActivity;
import chat.adapter.NotifyAdapter;
import chat.model.NotifyMessageModel;
import model.MessageNotify;
import model.Room;
import model.User;

/**
 * 新消息到达的通知
 * Created by 凯 on 2015/11/2.
 */
public class NotifyFragment extends Fragment {

    ListView lv_notify;
    NotifyAdapter adapter = new NotifyAdapter();

    NotifyMessageModel notifyMessageModel = new NotifyMessageModel();
    List<MessageNotify> messageNotifies = new ArrayList<MessageNotify>();

    MyNotifyBroadCastReceiver myNotifyBroadCastReceiver;
    {
        notifyMessageModel.setMessageNotifies(messageNotifies);
        adapter.setMessageNotifies(messageNotifies);
    }



    @Override
    public void onStart() {
        super.onStart();
        myNotifyBroadCastReceiver = new MyNotifyBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter("com.whu.zj.RoomMessage");
        intentFilter.addAction("com.whu.administrator.ws.userMessage");
        getActivity().registerReceiver(myNotifyBroadCastReceiver, intentFilter);
    }

    @Override
    public void onStop() {
        super.onStop();
        getActivity().unregisterReceiver(myNotifyBroadCastReceiver);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notify,null);
        initview(view);
        return view;
    }


    // 初始化view
    private void initview(View view) {
        lv_notify = (ListView) view.findViewById(R.id.id_lv_notify);
        lv_notify.setAdapter(adapter);

        lv_notify.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MessageNotify messageNotify = messageNotifies.get(position);
                // 群组消息
                if (messageNotify.getType().equals("5"))
                {
                    Room userRoom = new Room();
                    userRoom.setRoom_Name(messageNotify.getRealName());
                    userRoom.setRoom_ID(Long.parseLong(messageNotify.getNameid()));
                    Intent intent = new Intent(getActivity(), RoomChatActivity.class);
                    intent.putExtra("userRoom",userRoom);
                    getActivity().startActivity(intent);
                }
                else if (messageNotify.getType().equals("8"))
                {
                    User user = new User();
                    user.setUser_RealName(messageNotify.getRealName());
                    user.setUser_ID(messageNotify.getNameid());
                    Intent intent = new Intent(getActivity(), ChatActivity.class);
                    intent.putExtra("user",user);
                    getActivity().startActivity(intent);
                }

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        notifyMessageModel.init();
        adapter.notifyDataSetChanged();
    }

    class MyNotifyBroadCastReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            notifyMessageModel.init();
            adapter.notifyDataSetChanged();
        }
    }
}
